package com.cnhaimei.vod;

import java.util.ArrayList;
import java.util.List;

import android.R.integer;

import com.cnhaimei.vod.bean.Room;

/**
 * 单机版包间管理器 <br>
 * 
 * @author funwell <br>
 * @date 2013-7-27
 * 
 */
public class LocalRoomsManager {

	private final static LocalRoomsManager mG = new LocalRoomsManager();
	private List<Room> mRoomList = null;
	private int mSelectIndex = -1;
	
	private LocalRoomsManager() {
		mRoomList = new ArrayList<Room>();
	}

	public static LocalRoomsManager G() {
		return mG;
	}
	
	public void addRoom(Room room) {
		if (mRoomList != null) {
			for (Room item : mRoomList) {
				if (item.getNum().equals(room.getNum()) && item.getIp().equals(room.getIp())) {
					return ;
				}
			}
			
			mRoomList.add(room);
		}
	}
	
	public void deleteRoom(int index) {
		if (mRoomList != null) {
			mRoomList.remove(index);
		}
	}
	
	public void deleteRoom(Room room) {
		if (mRoomList != null) {
			for (Room item : mRoomList) {
				if (item.getNum().equals(room.getNum()) && item.getIp().equals(room.getIp())) {
					mRoomList.remove(item);
					break;
				}
			}
		}
	}
	
	public void clearRooms() {
		if (mRoomList != null) {
			mRoomList.clear();
			mSelectIndex = -1;
		}
	}
	
	public void setSelectedIndex(int index) {
		mSelectIndex = index;
	}
	
	public int getSelectedIndex() {
		return mSelectIndex;
	}
	
	public String getIp(int index) {
		if (index > -1 && index < mRoomList.size()) {
			return mRoomList.get(index).getIp();
		}
		
		return null;
	}
	
	public String getSelectedIp() {
		return getIp(getSelectedIndex());
	}
	
	public List<Room> getRoomList() {
		return mRoomList;
	}
	
	public List<String> getRoomNames() {
		List<String> nameList = new ArrayList<String>();
		for (int i = 0; i < mRoomList.size(); i++) {
			nameList.add(mRoomList.get(i).getNum());
		}
		return nameList;
	}
	
	public static Room parse(String strData) {
		Room room = null; 
		if (strData != null && strData.length() > 0) {
			int index = strData.indexOf(',');
			if (index > 0) {
				String strIp = strData.substring(0, index);
				String strRoomName = strData.substring(index + 1).replace(';', ' ').trim();

				room = new Room();
				room.setIp(strIp);
				room.setNum(strRoomName);
			}
		}
		
		return room;
	}
}
